﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using System;
using Microsoft.Win32;
using System.Security.Permissions;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace UColor.Core.Helpers {
	public class WinApiRegistryKey : Disposable {
		IntPtr handle;
		public WinApiRegistryKey(RegistryHive hive, string key, WinApiHelper.ResigtryAccess access) {
			handle = WinApiHelper.OpenRegistryKey(hive, key, access);
		}
		protected override void DisposeUnmanaged() {
			if(handle != IntPtr.Zero)
				WinApiHelper.CloseRegistryKey(handle);
			handle = IntPtr.Zero;
			base.DisposeUnmanaged();
		}
		public bool Exists { get { return handle != IntPtr.Zero; } }
		public string[] GetMultiSzValue(string name) {
			if(!Exists) return null;
			return WinApiHelper.ReadRegistryKeyMultiSzValue(handle, name);
		}
	}
	public class WinApiRegistryMultiKey : Disposable {
		List<WinApiRegistryKey> keys = new List<WinApiRegistryKey>();
		public WinApiRegistryMultiKey(RegistryHive hive, string key, WinApiHelper.ResigtryAccess access) {
			WinApiRegistryKey wkey = new WinApiRegistryKey(hive, key, access | WinApiHelper.ResigtryAccess.WOW64_32Key);
			if(wkey.Exists)
				keys.Add(wkey);
			wkey = new WinApiRegistryKey(hive, key, access | WinApiHelper.ResigtryAccess.WOW64_64Key);
			if(wkey.Exists)
				keys.Add(wkey);
		}
		protected override void DisposeManaged() {
			foreach(WinApiRegistryKey wkey in keys)
				wkey.Dispose();
			keys.Clear();
			base.DisposeManaged();
		}
		public bool Exists { get { return keys.Count > 0; } }
		public string[] GetMultiSzValue(string name) {
			foreach(WinApiRegistryKey wkey in keys) {
				string[] s = wkey.GetMultiSzValue(name);
				if(s != null) return s;
			}
			return null;
		}
	}
	public abstract class RegistryViewerBase {
		public abstract bool IsKeyExists(RegistryHive hive, string key);
		public abstract string[] GetMultiSzValue(RegistryHive hive, string key, string name);
	}
	public class RegistryViewer : RegistryViewerBase {
		public static RegistryViewer Current = new RegistryViewer();
		public override bool IsKeyExists(RegistryHive hive, string key) {
			using(WinApiRegistryMultiKey wkey = GetWKey(hive, key)) {
				return wkey.Exists;
			}
		}
		public override string[] GetMultiSzValue(RegistryHive hive, string key, string name) {
			using(WinApiRegistryMultiKey wkey = GetWKey(hive, key)) {
				return wkey.GetMultiSzValue(name);
			}
		}
		WinApiRegistryMultiKey GetWKey(RegistryHive hive, string key) {
			key = key.Replace('/', '\\');
			return new WinApiRegistryMultiKey(hive, key, WinApiHelper.ResigtryAccess.Read);
		}
	}
	public class SqlConnectionHelper {
		public static bool CheckSQLExpressConnection() {
			using(SqlConnection connection = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;")) {
				try {
					connection.Open();
				} catch {
					return false;
				}
				return true;
			}
		}
	}
}
